﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;
using JinianNet.JNCms.Data;

namespace JinianNet.JNCms.SQLiteDAL
{
    public class Tag : BaseDAL, ITag
    {
        public int Insert(string item)
        {
            if (JinianNet.Common.Utils.IsNullOrEmpty(item))
                return 0;
            Models.Tag model = new Models.Tag();
            if (model.ID<=0)
                model.ID = Db.CreateKey("Tag");
            model.Count = 1;
            model.Title = item;
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,8),
					new SQLiteParameter("@Count", DbType.Int32,8),
					new SQLiteParameter("@Title", DbType.String)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Count;
            parameters[2].Value = model.Title;

            return Db.Helper.ExecuteNonQuery(CommandType.Text, "insert into JNCms_Tag([ID],[Count],[Title]) values (@ID,@Count,@Title)", parameters);
        }

        public int Update(string item, int count)
        {
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Count", DbType.Int32,8),
					new SQLiteParameter("@Title", DbType.String)};
            parameters[0].Value = count;
            parameters[1].Value = item;

            return Db.Helper.ExecuteNonQuery(CommandType.Text, "update JNCms_Tag set [Count]=[Count]+@Count where [Title]=@Title", parameters);
        }

        public int Delete(int id)
        {
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = id;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "delete from JNCms_Tag where [ID]=@ID", parameters);
        }

        public List<Models.Links> GetList(int pageIndex, int pageSize)
        {
            List<Models.Links> list = new List<Models.Links>();
            if (pageSize > 0)
            {
                string sql;
                if (pageIndex == 1)
                    sql = pageSize.ToString();
                else
                    sql = string.Concat(Convert.ToString(pageSize * (pageIndex - 1)), ",", Convert.ToString(pageSize * pageIndex));

                using (IDataReader rd = Db.Helper.ExecuteReader( string.Concat("select [ID],[Count],[Title] from [JNCms_Tag] Order by [Count] desc", sql)))
                {
                    while (rd.Read())
                    {
                        list.Add(Db.Entity.PopulateEntity<Models.Links>(rd));
                    }
                }
            }
            return list;
        }

        public bool Exists(string item)
        {
            SQLiteParameter parameters = new SQLiteParameter("@Title", DbType.String);
            parameters.Value = item;

            return int.Parse(Db.Helper.ExecuteReader(CommandType.Text, "select count(ID) from JNCms_Tag where [Title]=@Title", parameters).ToString())>0;

        }
    }
}
